package jp.gr.jted;

import org.json.JSONArray;
import org.json.JSONObject;

import jp.gr.jted.bean.AppBean;
import jp.gr.jted.bean.Talk;
import jp.gr.jted.util.TedUtil;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MultiViewActivity extends Activity implements Runnable{

	private AppBean appBean = null;
	private TedUtil tedUtil = null;
	private LinearLayout footer;
	private Handler handler = null;
	private JSONArray firstLangSentenceList;
	private JSONArray secoundLangSentenceList;
	private LinearLayout transList;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// hide the title
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.multi_viewer);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


		this.appBean = (AppBean) this.getApplication();
		this.tedUtil = new TedUtil();
		this.handler = new Handler();
		this.transList = (LinearLayout)findViewById(R.id.trans_list);
		this.footer = (LinearLayout) getLayoutInflater().inflate(R.layout.talk_footer,null);

		Thread thread = new Thread(this);
		thread.start();
	}

	@Override
	public void run() {
		try{
			// Thumnail & Title
			Talk talk = this.appBean.getTalk();
			ImageView thumnail = (ImageView)findViewById(R.id.thumnail);
			thumnail.setImageBitmap((Bitmap)talk.getBitmap());
			TextView title = (TextView)findViewById(R.id.title);
			title.setText(talk.getTitle());
			handler.post(new Runnable(){
				@Override
				public void run() {
					transList.addView(footer);
				}
			});
			// Detail
			Talk talkDetail = tedUtil.getTalkDetail(talk.getUrl());
			JSONObject firstLang = new JSONObject(talkDetail.getFirstSubtitle());
			JSONObject secoundLang = new JSONObject(talkDetail.getSecoundSubtitle());
			this.firstLangSentenceList = firstLang.getJSONArray("captions");
			this.secoundLangSentenceList = secoundLang.getJSONArray("captions");
			handler.post(new Runnable(){
				@Override
				public void run() {
					transList.removeAllViews();
					LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					for(int idx=0;idx<firstLangSentenceList.length();idx++){
						try{
							JSONObject firstLangSentence = firstLangSentenceList.getJSONObject(idx);
							JSONObject secoundLangSentence = secoundLangSentenceList.getJSONObject(idx);
							LinearLayout row = (LinearLayout)inflater.inflate(R.layout.sentence,null);
							TextView first = (TextView)row.findViewById(R.id.first);
							first.setText(firstLangSentence.getString("content").replaceAll("\n",""));
							TextView secound = (TextView)row.findViewById(R.id.secound);
							secound.setText(secoundLangSentence.getString("content").replaceAll("\n",""));
							transList.addView(row);
						}catch(Exception e){
							Log.e(Definition.TAG,e.getMessage());
						}
					}
				}
			});
		}catch(Exception e){
			Log.e(Definition.TAG,e.getMessage());
		}
	}
}